/*
 *
 *      Copyright (c) 2018-2025, shenghua All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: shenghua
 *
 */

package com.chen.base.module.log.aspect;

import cn.hutool.core.util.StrUtil;
import com.chen.base.module.log.annotation.SysLog;
import com.chen.base.module.log.component.SysLogHandlerInter;
import com.chen.base.module.log.dto.AccessLogDTO;
import com.chen.base.module.log.util.AccessLogUtil;
import com.chen.base.module.log.util.LogTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.EvaluationContext;

import javax.annotation.Resource;

/**
 * 操作日志使用spring event异步入库
 *
 * @author L.cm
 */
@Slf4j
@Aspect
@RequiredArgsConstructor
public class SysLogAop {

	@Resource
	private SysLogHandlerInter sysLogHandlerInter;
	@Value("${spring.application.name}")
	private String serverName;

	@Around("@annotation(sysLog)")
	@SneakyThrows
	public Object around(ProceedingJoinPoint point, SysLog sysLog) {
		String strClassName = point.getTarget().getClass().getName();
		String strMethodName = point.getSignature().getName();
		log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);

		String value = sysLog.value();
		String expression = sysLog.expression();
		// 当前表达式存在 SPEL，会覆盖 value 的值
		if (StrUtil.isNotBlank(expression)) {
			// 解析SPEL
			MethodSignature signature = (MethodSignature) point.getSignature();
			EvaluationContext context = AccessLogUtil.getContext(point.getArgs(), signature.getMethod());
			try {
				value = AccessLogUtil.getValue(context, expression, String.class);
			}
			catch (Exception e) {
				// SPEL 表达式异常，获取 value 的值
				log.error("@SysLog 解析SPEL {} 异常", expression);
			}
		}

		AccessLogDTO logVo = AccessLogUtil.getSysLog();
		logVo.setTitle(value);
		logVo.setServiceId(serverName);

		// 发送异步日志事件
		Long startTime = System.currentTimeMillis();
		Object obj;

		try {
			obj = point.proceed();

		} catch (Exception e) {
			logVo.setType(LogTypeEnum.ERROR.getType());
			logVo.setException(e.getMessage());
			throw e;

		} finally {
			Long endTime = System.currentTimeMillis();
			logVo.setTime(endTime - startTime);
			sysLogHandlerInter.logHandler(logVo);
		}

		return obj;
	}


}
